< previous page page_229 next page >

Page 229
e10a6a0a59f035e870d90cca05b85fce.gif
Private NextFrame As Integer
Private NumImages As Integer

' Display the next frame.
Private Sub tmrFrame_Timer()
    ' Display the next frame.
    picCanvas.Picture = picFrame(NextImage).Image

    ' Display the next frame next.
    NextImage = NextImage + 1

    ' If that was the last frame, stop.
    If NextImage >= NumImages Then tmrFrame.Enabled = False
End Sub
Example program PlayTmr uses this technique to display a series of bitmaps. Load the bitmaps as you would for program PlayFast. Enter the number of frames per second you want displayed and click the Start button.
This technique gives you greater control over the animation speed than the previous technique does. If you set the timer control's Interval property to 50, the program displays roughly 20 frames per second if it can run fast enough. If the computer is too slow to display the images that quickly, it displays them as fast as it can.
Unfortunately, using a timer control has a few drawbacks. First, the timer control imposes some overhead and slows the program down. The same 133MHz Pentium processor that displayed 395 frames per second using program PlayFast could display only 96 frames per second using program PlayTmr. That is still more than fast enough for smooth animation, but on a slower computer, it might make a difference.
A second problem with timers is that they do not count the time spent during the Timer event. Suppose you have a timer set to display an image every 200 milliseconds, and the Timer event takes 50 milliseconds to display a picture. Each Timer event does not occur until at least 200 milliseconds after the previous one finishes. An event may take even longer if other programs are using the system heavily. That means more than 250 milliseconds will pass between the start of one Timer event and the start of the next, rather than the 200 milliseconds you expect.
This unwanted timing adjustment is merely inconvenient if you can display each image in roughly the same amount of time. The images will be displayed at a slightly slower rate than you would like, but after some testing, you could adjust the timer's interval to 150 milliseconds and get reasonably close to the timing you want.
The problem is greater if different frames take different lengths of time to display. Suppose your program generates each image before displaying it. For instance, the images might show a cube being rotated in three dimensions with the program calculating a new position for the cube for each frame. In that case, it might take a different amount of time to generate each frame. To make the timer control present images at a constant rate, you would need to recalculate the timer's Interval property every time you displayed an image. In the Timer event, you would need to follow these steps.

 
< previous page page_229 next page >